<script>
import { getAction, postAction } from '@api/manage'
import InboundRecordLabelList from './InboundRecordLabelList.vue'

export default {
  name: 'InboundRecordLabelGeneratePrintModal',
  components: { InboundRecordLabelList },
  data() {
    return {
      title: '',
      width: 800,
      visible: false,
      disableSubmit: false,
      model: {},
      labelList: [],
      selectionRows: [],
      // 批次号数量
      labelNum: 1
    }
  },
  methods: {
    print(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    handleLabelNum() {
      const num = this.labelNum
      if (isNaN(num) || num < 1) {
        this.labelList = []
        return
      }
      this.generateLabels()
    },
    generateLabels() {
      getAction('/wms/inbound/inbound-order/generate-labels', {
        itemId: this.model.inboundOrderItemId,
        quantity: this.labelNum
      }).then((res) => {
        let { result } = res
        if (result) {
          this.labelList = result
        }
      })
    },
    handleLabelSelectChange(selectedRowKeys, selectionRows) {
      this.selectionRows = selectionRows
    },
    handleOk() {
      if (!this.selectionRows || this.selectionRows.length <= 0) {
        this.$message.error('请选择要打印的批次')
        return
      }
      const ids = this.selectionRows.map((item) => item.id).join('\',\'')
      window.open(`${window._CONFIG['domianURL']}/jmreport/view/1099651769747447808?id=${ids}`, '_blank')
      this.$emit('ok')
      this.close()
    },
    submitCallback() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.labelList = []
      this.selectionRows = []
      this.labelNum = 1
      this.$emit('close')
      this.visible = false
    }
  }
}
</script>

<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    fullscreen
    okText="打印"
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    cancelText="关闭"
    @cancel="handleCancel">
    <a-form layout="inline">
      <a-form-item
        label="批次号数量"
      >
        <a-input v-model="labelNum" type="number" min="1" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="handleLabelNum">生成标签</a-button>
      </a-form-item>
    </a-form>
    <inbound-record-label-list :data-source="labelList" @change="handleLabelSelectChange" />
  </j-modal>
</template>

<style scoped>
</style>